<template>
  <div class="top">
    <div class="left">
      <span class="lbtn" @click="goHome">首页</span>
    </div>
    <div class="center">
      <div class="title">可视化</div>
    </div>
    <div class="right">
      <span class="rbtn">统计报告</span>
      <span class="time">当前时间 {{ currentTime }}</span>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
import { onBeforeUnmount, onMounted, ref } from 'vue'
import moment from 'moment'

// 获取路由器对象
const $router = useRouter()
/**
 * 按钮的点击返回首页
 */
const goHome = () => {
  $router.push('/home')
}
// 存储当前时间
const currentTime = ref(moment().format('YYYY-MM-DD HH:mm:ss'))
// 定时器，组件销毁时得清除 !!!
let timer = ref<number>(0)
// 组件挂载完毕更新当前的时间
onMounted(() => {
  timer.value = setInterval(() => {
    currentTime.value = moment().format('YYYY-MM-DD HH:mm:ss')
  }, 1000)
})
// 组件卸载前清除定时器 !!!
onBeforeUnmount(() => {
  clearInterval(timer.value)
})
</script>
<style scoped lang="scss">
.top {
  width: 100%;
  height: 40px;
  display: flex;

  .left {
    flex: 1;
    background: url('../../images/dataScreen-header-left-bg.png') no-repeat;
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: right;

    .lbtn {
      width: 130px;
      height: 40px;
      background: url('../../images/dataScreen-header-btn-bg-l.png') no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px;
      font-size: 20px;
      color: #29fcff;
    }
  }

  .center {
    flex: 2;

    .title {
      width: 100%;
      height: 74px;
      background: url('../../images/dataScreen-header-center-bg.png') no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 74px;
      color: #29fcff;
      font-size: 30px;
    }
  }

  .right {
    flex: 1;
    background: url('../../images/dataScreen-header-right-bg.png') no-repeat;
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .rbtn {
      width: 150px;
      height: 40px;
      background: url('../../images/dataScreen-header-btn-bg-r.png') no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px;
      font-size: 20px;
      color: #29fcff;
    }

    .time {
      margin-right: 30px;
      color: #29fcff;
    }
  }
}
</style>
